﻿using SCG.General;
using System;
using System.Net;

namespace SCG.SolarVengeanceEngine.StarShips
{
    //BeamShips
    public class BeamShip : StarShip
    {
        //constructor
        public BeamShip(SVGame game, Player owner, int engines, int beamRange, StarSystem origin)
            : base(game, owner, engines, beamRange, origin)
        {
        }

        //Ship name
        public override string Name
        {
            get
            {
                return "BeamShip";
            }
        }

        //description
        public override string Description
        {
            get
            {
                return "BeamShips can manually target and destroy an enemy StarShip with their Beam.  The BeamShip's range is determined by its Beam Range rating.";
            }
        }

        //ship type
        public override StarShipType ShipType
        {
            get
            {
                return StarShipType.BeamShip;
            }
        }

        //The target that they shot with the beam this turn
        public StarShip BeamTarget { get; set; }

        //They can target any enemy StarShip
        public override bool CanExecuteOrder(OrderType ot)
        {
            switch (ot)
            {
                case OrderType.TargetStarShip:
                    return true;
                default:
                    return base.CanExecuteOrder(ot);
            }
        }
        public override bool CanTargetStarShip(StarShip ship, bool respectAlliance)
        {
            return ship.Owner != Owner;
        }

        //Clear the beam target every impulse
        protected internal override void CommonPreProcessing()
        {
            BeamTarget = null;
        }

        //They fire the beam from a distance, if our of range, move toward target
        protected internal override void TargetStarShip(StarShip target)
        {
            //If the target is visible and in range, destroy it!
            if (target.VisibleTo[Owner] && target.Owner != Owner)
            {
                if (Distance(target) <= Value)
                {
                    //notify host
                    if (Game.Host != null)
                        Game.Host.BeamAttack(this, target);

                    //submit state change letting clients know there was a beam attack
                    BeamTarget = target;
                    Game.SubmitStateChange(StateChange.BeamAttack, this, target);

                    //destroy the target
                    Game.DestroyStarShip(target);
                }
                else
                    Game.MoveShipTowardPoint(this, target.Location);
            }        
        }

        //draw beam range
        public override SCGColor RadiusColor
        {
            get
            {
                return BeamRangeColor;
            }
        }

        //private members
        private static SCGColor BeamRangeColor = new SCGColor(96, 255, 0, 255);
    }
}
